package com.example.contactapp.adapters;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.contactapp.R;
import com.example.contactapp.objects.ContactObject;
import com.example.contactapp.views.SectionListItem;

@SuppressLint("SimpleDateFormat")
public class ContactAdapter extends ArrayAdapter<SectionListItem> implements OnClickListener{
	private ArrayList<Integer> myArrayChecked;
	private Activity myActivity;
    public ContactAdapter(Activity context, ArrayList<SectionListItem> arrays) {
        super(context, R.layout.item_list_contact, arrays);
        myArrayChecked = new ArrayList<>();
        myActivity = context;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(
                    R.layout.item_list_contact, null);
        } 
        
        TextView tvName = (TextView) convertView
                .findViewById(R.id.tv_name);
        TextView tvAddress = (TextView) convertView
                .findViewById(R.id.tv_address);
        ImageView imgAvatar = (ImageView) convertView.findViewById(R.id.img_avatar);
        
        ContactObject contact = ((SectionListItem)getItem(position)).item;
        
        tvAddress.setText(contact.getAddress());
        if(contact.getName() != null && contact.getName().length() > 0){
        	tvName.setText(contact.getName());
        }else{
        	tvName.setText(contact.getAddress());
        }
        if(contact.getAvatar() != null){
        	imgAvatar.setImageURI(Uri.parse(contact.getAvatar()));
        }else{
        	imgAvatar.setImageDrawable(myActivity.getResources().getDrawable(R.drawable.ic_contact_picture));
        }
        
        convertView.setTag(contact);
        convertView.setOnClickListener(this);
        return convertView;
    }

    public ArrayList<Integer> getArrayChecks(){
    	return myArrayChecked;
    }
    private static class ViewHolder {
        TextView tvName;
        TextView tvAddress;
        ImageView imgAvatar;
    }
    
    
	public int getCountChecked(){
		return myArrayChecked.size();
	}
	public void clearChecked(){
		myArrayChecked.clear();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	}

}
